package com.relife.sys.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.relife.common.model.AjaxJson;
import com.relife.common.model.Page;
import com.relife.common.utils.StringUtil;
import com.relife.core.easyui.Easyui;
import com.relife.core.entity.RoleEntity;
import com.relife.core.entity.UserRoleEntity;
import com.relife.core.qbc.QbcQuery;
import com.relife.sys.service.RoleService;
import org.hibernate.sql.JoinType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;



@Controller
@RequestMapping("sys/role")
public class RoleController extends BaseController {
	@Autowired
	private RoleService roleService;

	@RequestMapping
	public String roleList(HttpServletRequest req) {
		return "sys/role/roleList";
	}

	/**
	 * 数据列表
	 * 
	 * @author ANT
	 * @date 2016-07-01 10:47:51
	 * @param page
	 */
	@RequestMapping
	public void listData(RoleEntity role, Page page, HttpServletResponse res) {
		QbcQuery qbc = new QbcQuery(role);
		service.getPage(qbc, page);
		Easyui.datagrid(page, res);
	}

	/**
	 * 
	 * @author WU
	 * @date 2016年12月23日 下午4:59:33
	 * @param role
	 * @return
	 */
	@ResponseBody
	@RequestMapping
	public AjaxJson saveRole(RoleEntity role) {
		AjaxJson j = new AjaxJson();
		try {
			roleService.save(role);
		} catch (Exception e) {
			j.setException(e);
			e.printStackTrace();
		}
		return j;

	}

	/**
	 * 
	 * @author WU
	 * @date 2017年3月17日 下午4:12:02
	 * @param req
	 * @return
	 */
	@RequestMapping
	public String roleUserList(HttpServletRequest req) {
		String id = req.getParameter("roleId");
		if (StringUtil.isNotEmpty(id)) {
			req.setAttribute("role", service.get(RoleEntity.class, id));
		}
		return "sys/role/roleUserList";
	}

	/**
	 * 
	 * @author WU
	 * @date 2017年3月17日 下午4:12:41
	 * @param userRole
	 * @param page
	 * @param res
	 */
	@RequestMapping
	public void roleUserListGrid(UserRoleEntity userRole, Page page, HttpServletResponse res) {
		QbcQuery qbc = new QbcQuery(userRole);
		qbc.createAlias("user", JoinType.INNER_JOIN);
		qbc.createAlias("role", JoinType.INNER_JOIN);
		qbc.createAlias("depart", JoinType.LEFT_OUTER_JOIN);
		service.getPage(qbc, page);
		Easyui.datagrid(page, res);
	}

}
